package com.xfly.app.wxapi;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.IntDef;

import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Created by shaohui on 2016/11/18.
 */

public class WxShareInstance {

    /**
     * 微信分享限制thumb image必须小于32Kb，否则点击分享会没有反应
     */

    private IWXAPI mIWXAPI;
    private static shareStatu mShareStatu;
    private static ShareListener mShareListener;
    public static final int SHARE_SUCCESS = 0;//分享成功
    public static final int SHARE_FAIL = -4;//分享失败
    public static final int SHARE_CANCEL = -2;//分享取消

    @IntDef({SHARE_SUCCESS,SHARE_FAIL,SHARE_CANCEL})
    @Retention(RetentionPolicy.SOURCE)
    public @interface ShareResult {
    }

    private static final int WX_THUMB_SIZE = 120;
    public WxShareInstance(Context context, String appId) {
        mIWXAPI = WXAPIFactory.createWXAPI(context, appId, false);
        mIWXAPI.registerApp(appId);
    }
    public interface shareStatu{
        void getStatu();
    }
    public void getShareStatu(shareStatu mShareStatu){
        this.mShareStatu=mShareStatu;
    }
    public static void show(){
        if(mShareStatu!=null) {
            mShareStatu.getStatu();
            mShareStatu=null;
        }
    }

    public static void notifyShareResult(@ShareResult int resultCode){
        if (null != mShareListener) {
            switch (resultCode) {
                case SHARE_SUCCESS:
                    mShareListener.shareSuccess();
                    break;
                case SHARE_FAIL:
                    mShareListener.shareFailure(null);
                    break;
                case SHARE_CANCEL:
                    mShareListener.shareCancel();
                    break;
            }
        }
    }


    public void shareText(int platform, String text, Context activity) {
        //初始化一个WXTextObject对象，填写分享的文本内容
        WXTextObject textObject = new WXTextObject();
        textObject.text = text;
        //对象初始化一个WXMediaMessage对象
        WXMediaMessage message = new WXMediaMessage();
        message.mediaObject = textObject;
        message.description = text;
        sendMessage(platform, message, buildTransaction("text"));
    }
    public void shareMedia(
            final int platform, final String title, final String targetUrl, final String summary,
            final ShareImageObject shareImageObject, final Context activity) {

    }
    public void shareImage(final int platform, final ShareImageObject shareImageObject,
            final Context activity) {
    }
    //如果 不想关系分享回调结果 ShareListener可以为Null
    public void shareWeb(int platform, String title, String content, String url, Bitmap bitmap,
                         Context activity, ShareListener shareListener) {
        this.mShareListener = shareListener;
        //初始化一个WXWebpageObject对象，填写分享的网页地址
        WXWebpageObject webObject = new WXWebpageObject();
        webObject.webpageUrl=url;
        //对象初始化一个WXMediaMessage对象
        WXMediaMessage message = new WXMediaMessage(webObject);
        message.title = title;//标题
        message.description = content;//描述
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, WX_THUMB_SIZE, WX_THUMB_SIZE, true);
//        bitmap.recycle();
        message.thumbData = ImageUtil.bitmap2Bytes(thumbBmp);
        sendMessage(platform, message, buildTransaction("webpage"));
    }

    public void shareImg(int platform, String title, String content, String url, Bitmap bitmap,
                         Context activity, ShareListener shareListener) {
        if (null != bitmap) {
            WXImageObject imageObject = new WXImageObject(bitmap);
            WXMediaMessage message = new WXMediaMessage();
            message.mediaObject = imageObject;
            Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, WX_THUMB_SIZE, WX_THUMB_SIZE, true);
            message.thumbData = ImageUtil.bitmap2Bytes(thumbBmp);
            sendMessage(platform, message, buildTransaction("webpage"));
        }
    }

    private void sendMessage(int platform, WXMediaMessage message, String transaction) {
        //构造一个RQ transaction用户标示类型
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = transaction;
        req.message = message;
        req.scene = platform == SharePlatform.WX_TIMELINE ? SendMessageToWX.Req.WXSceneTimeline
                : SendMessageToWX.Req.WXSceneSession;
        mIWXAPI.sendReq(req);
    }

    private String buildTransaction(String type) {
        return System.currentTimeMillis() + type;
    }


}
